package com.tom.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tom.util.HttpRequest;
import com.tom.util.JDBCUtil;

public class LanJingJinRong {

	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月11日
	 * @TODO 获取投资信息
	 */
	public static void work(){
		ArrayList<String> list = new ArrayList<String>();
		try {
			String url = "https://www.lanjingjr.com/loanObject/investmentRecord.do";
			ArrayList<String> arrayList = JDBCUtil.getUrls("qss_lanjjr_bussiness_data");
			for (int i = 0; i < arrayList.size(); i++) {
				String param = arrayList.get(i);
				String loanId = param.replaceAll("https://www.lanjingjr.com/loanObject/openLoanDetails/", "").replaceAll(".html", "");
				param = "loanId="+loanId+"&currPage=1";
				String result = HttpRequest.sentPost(url, param);
				//System.out.println(result);
				JSONObject jsonObject = JSONObject.parseObject(result);
				//System.out.println(jsonObject);
				JSONObject object = jsonObject.getJSONObject("ajaxPage");
				//System.out.println(object);
				int totalPage = object.getIntValue("totalPage");
				String tzxx = "";
				for (int j = 1; j <= totalPage; j++) {
					param = "loanId="+loanId+"&currPage="+j;
					result = HttpRequest.sentPost(url, param);
					jsonObject = JSONObject.parseObject(result);
					JSONArray array = jsonObject.getJSONArray("investmentRecordList");
					for (int k = 0; k < array.size(); k++) {
						jsonObject = array.getJSONObject(k);
						//System.out.println(jsonObject);
						String LoginName = jsonObject.getString("agreementLoginName");
						String amount = jsonObject.getString("amount");
						String bidTimeString = jsonObject.getString("bidTimeString");
						String terminal = jsonObject.getString("terminal");
						tzxx += LoginName+"##"+amount+"##"+bidTimeString+"##"+terminal+"==";
					}
				}
				tzxx = tzxx.substring(0, tzxx.length()-2);
				System.out.println(tzxx);
				String sql = "update qss_lanjjr_bussiness_data set tzxx ="+"'"+tzxx+"'"+" where url = "+"'"+arrayList.get(i)+"'";
				list.add(sql);
			}
			JDBCUtil.insertData(list);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	
	
}
